package leetcode;

/**
 * Created by 林剑 on 2016/10/15.
 */
public class leetcode414 {
    public int thirdMax(int[] nums) {
        int max, mid, small, count;
        max = mid = small = Integer.MIN_VALUE;//初始化为最小值
        count = 0;

        for( int x: nums) {
            if( x == max || x == mid ) {
                continue;
            }

            if (x > max) {
                small = mid;
                mid = max;
                max = x;
                count++;
            } else if( x > mid) {
                small = mid;
                mid = x;
                count++;
            } else if ( x >= small) {//为了防止Integer.MIN_VALUE被漏掉
                small = x;
                count++;
            }
        }
        if( count >= 3) {
            return small;
        } else {
            return max;
        }
    }

    public static void main(String[] args) {
        leetcode414 leetcode414 = new leetcode414();
        int[] a = {1,2,Integer.MIN_VALUE};
        System.out.println(leetcode414.thirdMax(a));
    }
}
